package com.global.variable.common.web.ext;

import com.global.variable.common.web.interceptor.SecurityInterceptorProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.util.MapUtils;
import org.thymeleaf.util.StringUtils;

import java.util.Map;

/**
 * company 重庆庆云石油工程技术有限责任公司
 * FileName CustomPostProcessor
 * Package com.global.variable.common.web.ext
 * Description
 * author zjh
 * create 2019-01-22 下午 5:26
 * version V1.0
 */
@Component
public class CustomPostProcessor implements BeanPostProcessor {

    @Autowired
    private SecurityInterceptorProperties securityInterceptorProperties;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof ThymeleafViewResolver){
            Map<String, String> platforms = securityInterceptorProperties.getPlatforms();
            if(!MapUtils.isEmpty(securityInterceptorProperties.getPlatforms())){
                if(!StringUtils.isEmpty(securityInterceptorProperties.getJsActive())){
                    platforms.put("jsActive",securityInterceptorProperties.getJsActive());
                }
                ((ThymeleafViewResolver)bean).setStaticVariables(platforms);
            }

        }
        return bean;
    }
}
